package org.telegram.bot.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.ruoyi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.telegram.bot.domain.BotMenuLanguage;
import org.telegram.bot.mapper.BotMenuLanguageMapper;
import org.telegram.bot.service.IBotMenuLanguageService;

import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 菜单语言Service业务层处理
 *
 * @author wyx
 * @date 2023-05-31
 */
@Slf4j
@Service
public class BotMenuLanguageServiceImpl implements IBotMenuLanguageService
{
    @Resource
    private BotMenuLanguageMapper botMenuLanguageMapper;

    /**
     * 查询菜单语言
     *
     * @param id 菜单语言主键
     * @return 菜单语言
     */
    @Override
    public BotMenuLanguage selectBotMenuLanguageById(Long id)
    {
        return botMenuLanguageMapper.selectById(id);
    }

    /**
     * 查询菜单语言列表
     *
     * @param botMenuLanguage 菜单语言
     * @return 菜单语言
     */
    @Override
    public List<BotMenuLanguage> selectBotMenuLanguageList(BotMenuLanguage botMenuLanguage)
    {
        return botMenuLanguageMapper.selectBotMenuLanguageList(botMenuLanguage);
    }

    /**
     * 新增菜单语言
     *
     * @param botMenuLanguage 菜单语言
     * @return 结果
     */
    @Override
    public int insertBotMenuLanguage(BotMenuLanguage botMenuLanguage)
    {
        if(botMenuLanguage.getMenuId().equals(0)){
            throw new ServiceException("所属菜单不能为顶级节点");
        }
        if(languageExist(botMenuLanguage.getMenuId(),botMenuLanguage.getType(),null)){
            throw new ServiceException("语言类型已存在");
        }
        return botMenuLanguageMapper.insert(botMenuLanguage);
    }

    /**
     * 修改菜单语言
     *
     * @param botMenuLanguage 菜单语言
     * @return 结果
     */
    @Override
    public int updateBotMenuLanguage(BotMenuLanguage botMenuLanguage)
    {
        if(botMenuLanguage.getMenuId().equals(0)){
            throw new ServiceException("所属菜单不能为顶级节点");
        }
        if(languageExist(botMenuLanguage.getMenuId(),botMenuLanguage.getType(),botMenuLanguage.getId())){
            throw new ServiceException("语言类型已存在");
        }
        return botMenuLanguageMapper.updateById(botMenuLanguage);
    }

    /**
     * 批量删除菜单语言
     *
     * @param ids 需要删除的菜单语言主键
     * @return 结果
     */
    @Override
    public int deleteBotMenuLanguageByIds(Long[] ids)
    {
        return botMenuLanguageMapper.deleteByIds(Arrays.asList(ids));
    }

    /**
     * 删除菜单语言信息
     *
     * @param id 菜单语言主键
     * @return 结果
     */
    @Override
    public int deleteBotMenuLanguageById(Long id)
    {
        return botMenuLanguageMapper.deleteById(id);
    }

    /**
     * 判断菜单类型语言是否存在
     * @param menuId 菜单ID
     * @param language 语言类型字典类型bot_menu_lanuage
     * @return
     */
    public boolean languageExist(Long menuId,String language,String languageId){
        return SqlHelper.retBool(botMenuLanguageMapper.languageExist(menuId,language,languageId));
    }
}
